<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="停车场ID" prop="scParkingLotId">
        <el-input
          v-model="queryParams.scParkingLotId"
          placeholder="请输入停车场ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单编号" prop="scOrderNumber">
        <el-input
          v-model="queryParams.scOrderNumber"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否支付" prop="scIsPay">
        <el-select v-model="queryParams.scIsPay" placeholder="请选择是否支付" clearable>
          <el-option
            v-for="dict in dict.type.sc_is_pay"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="订单完成时间">
        <el-date-picker
          v-model="daterangeScCompletionTime"
          style="width: 240px"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['orders:parkingOrder:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['orders:parkingOrder:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['orders:parkingOrder:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['orders:parkingOrder:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="parkingOrderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="停车订单ID" align="center" prop="scOrderId" />
      <el-table-column label="停车场ID" align="center" prop="scParkingLotId" />
      <el-table-column label="订单编号" align="center" prop="scOrderNumber" />
      <el-table-column label="订单所属用户ID" align="center" prop="scUserId" />
      <el-table-column label="车辆ID" align="center" prop="scCarId" />
      <el-table-column label="停车位ID" align="center" prop="scSpaceId" />
      <el-table-column label="车辆进场时间" align="center" prop="scEntryTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.scEntryTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="车辆离场时间" align="center" prop="scExitTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.scExitTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="停车订单的总费用" align="center" prop="scTotalFee" />
      <el-table-column label="是否支付" align="center" prop="scIsPay">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sc_is_pay" :value="scope.row.scIsPay"/>
        </template>
      </el-table-column>
      <el-table-column label="优惠券ID" align="center" prop="scCouponId" />
      <el-table-column label="是否是会员" align="center" prop="scIsMember">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sc_is_member" :value="scope.row.scIsMember"/>
        </template>
      </el-table-column>
      <el-table-column label="实际支付金额" align="center" prop="scPayAmount" />
      <el-table-column label="订单完成时间" align="center" prop="scCompletionTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.scCompletionTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['orders:parkingOrder:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['orders:parkingOrder:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改停车订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="停车场ID" prop="scParkingLotId">
          <el-input v-model="form.scParkingLotId" placeholder="请输入停车场ID" />
        </el-form-item>
        <el-form-item label="订单编号" prop="scOrderNumber">
          <el-input v-model="form.scOrderNumber" placeholder="请输入订单编号" />
        </el-form-item>
        <el-form-item label="订单所属用户ID" prop="scUserId">
          <el-input v-model="form.scUserId" placeholder="请输入订单所属用户ID" />
        </el-form-item>
        <el-form-item label="车辆ID" prop="scCarId">
          <el-input v-model="form.scCarId" placeholder="请输入车辆ID" />
        </el-form-item>
        <el-form-item label="停车位ID" prop="scSpaceId">
          <el-input v-model="form.scSpaceId" placeholder="请输入停车位ID" />
        </el-form-item>
        <el-form-item label="车辆进场时间" prop="scEntryTime">
          <el-date-picker clearable
            v-model="form.scEntryTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择车辆进场时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="车辆离场时间" prop="scExitTime">
          <el-date-picker clearable
            v-model="form.scExitTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择车辆离场时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="停车订单的总费用" prop="scTotalFee">
          <el-input v-model="form.scTotalFee" placeholder="请输入停车订单的总费用" />
        </el-form-item>
        <el-form-item label="是否支付" prop="scIsPay">
          <el-select v-model="form.scIsPay" placeholder="请选择是否支付">
            <el-option
              v-for="dict in dict.type.sc_is_pay"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="优惠券ID" prop="scCouponId">
          <el-input v-model="form.scCouponId" placeholder="请输入优惠券ID" />
        </el-form-item>
        <el-form-item label="是否是会员" prop="scIsMember">
          <el-select v-model="form.scIsMember" placeholder="请选择是否是会员">
            <el-option
              v-for="dict in dict.type.sc_is_member"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="实际支付金额" prop="scPayAmount">
          <el-input v-model="form.scPayAmount" placeholder="请输入实际支付金额" />
        </el-form-item>
        <el-form-item label="订单完成时间" prop="scCompletionTime">
          <el-date-picker clearable
            v-model="form.scCompletionTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择订单完成时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listParkingOrder, getParkingOrder, delParkingOrder, addParkingOrder, updateParkingOrder } from "@/api/orders/parkingOrder";

export default {
  name: "ParkingOrder",
  dicts: ['sc_is_member', 'sc_is_pay'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 停车订单表格数据
      parkingOrderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 订单完成时间时间范围
      daterangeScCompletionTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        scParkingLotId: null,
        scOrderNumber: null,
        scIsPay: null,
        scCompletionTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        scOrderId: [
          { required: true, message: "停车订单ID不能为空", trigger: "blur" }
        ],
        scParkingLotId: [
          { required: true, message: "停车场ID不能为空", trigger: "blur" }
        ],
        scOrderNumber: [
          { required: true, message: "订单编号不能为空", trigger: "blur" }
        ],
        scUserId: [
          { required: true, message: "订单所属用户ID不能为空", trigger: "blur" }
        ],
        scCarId: [
          { required: true, message: "车辆ID不能为空", trigger: "blur" }
        ],
        scSpaceId: [
          { required: true, message: "停车位ID不能为空", trigger: "blur" }
        ],
        scEntryTime: [
          { required: true, message: "车辆进场时间不能为空", trigger: "blur" }
        ],
        scExitTime: [
          { required: true, message: "车辆离场时间不能为空", trigger: "blur" }
        ],
        scTotalFee: [
          { required: true, message: "停车订单的总费用不能为空", trigger: "blur" }
        ],
        scIsPay: [
          { required: true, message: "是否支付不能为空", trigger: "change" }
        ],
        scCouponId: [
          { required: true, message: "优惠券ID不能为空", trigger: "blur" }
        ],
        scIsMember: [
          { required: true, message: "是否是会员不能为空", trigger: "change" }
        ],
        scPayAmount: [
          { required: true, message: "实际支付金额不能为空", trigger: "blur" }
        ],
        scCompletionTime: [
          { required: true, message: "订单完成时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询停车订单列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeScCompletionTime && '' != this.daterangeScCompletionTime) {
        this.queryParams.params["beginScCompletionTime"] = this.daterangeScCompletionTime[0];
        this.queryParams.params["endScCompletionTime"] = this.daterangeScCompletionTime[1];
      }
      listParkingOrder(this.queryParams).then(response => {
        this.parkingOrderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        scOrderId: null,
        scParkingLotId: null,
        scOrderNumber: null,
        scUserId: null,
        scCarId: null,
        scSpaceId: null,
        scEntryTime: null,
        scExitTime: null,
        scTotalFee: null,
        scIsPay: null,
        scCouponId: null,
        scIsMember: null,
        scPayAmount: null,
        scCompletionTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeScCompletionTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.scOrderId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加停车订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const scOrderId = row.scOrderId || this.ids
      getParkingOrder(scOrderId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改停车订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.scOrderId != null) {
            updateParkingOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addParkingOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const scOrderIds = row.scOrderId || this.ids;
      this.$modal.confirm('是否确认删除停车订单编号为"' + scOrderIds + '"的数据项？').then(function() {
        return delParkingOrder(scOrderIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('orders/parkingOrder/export', {
        ...this.queryParams
      }, `parkingOrder_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
